/* logger.c */

#include <stdio.h>
#include "Logger.h"
#include "httpConnectionHandler.h"
#include "configFile.h"


/* Function: logConnection
* Usage: logConnection(conn);
* -------------------------------- 
* This function will open the log-file and log the current connection. It will open the log-file and make a log consisting of the 
* following information: IP-adress, date and time, HTTP command, HTTP status-code and the amount of bytes sent.
*/ 
void logConnection(httpConnectionT *conn) {
	char *ipAddr;
	FILE *outfile;
	
	ipAddr = inet_ntoa(conn->clientInfo.sin_addr);
	
	outfile = fopen(getLogFilePath(), "a");
	if (outfile == NULL) {
		outfile = fopen(getLogFilePath(), "w");
	}

	fprintf(outfile, "%s [%s +0100] \"%s %s %s\" %d %d\r\n", ipAddr, conn->dateTime, conn->httpAction, conn->requestedFile, conn->protocolVersion, conn->statusCode, conn->bufferSize);
	printf("Connection logged\n");

	fclose(outfile);
}